package com.tcoiss.common.component.service;

import com.tcoiss.common.core.constant.ServiceNameConstants;
import com.tcoiss.common.core.domain.R;
import com.tcoiss.common.core.enums.ErroEnums;
import com.tcoiss.common.core.exception.CustomException;
import com.tcoiss.common.component.api.model.ApiParam;
import com.tcoiss.common.component.servicehelper.DispatchServiceHelper;
import org.springframework.stereotype.Component;

import java.util.Map;


/**
 * API调用服务类型
 * 根据api编码，api参数调用
 * 并解析返回的数据为MAP
 */
@Component
public class ApiHelperService {


    public Map<String,Object> executeApi(String apiCode,Object param){
        ApiParam apiParam = new ApiParam();
        apiParam.setApiCode(apiCode);
        apiParam.setParam(param);
        R<Map<String, Object>> r = DispatchServiceHelper.invokeBizService("tcoiss", ServiceNameConstants.WEBAPI_SERVICE,"executeApi",apiParam);
        if(r.getCode()==200){
            return r.getData();
        }
        throw new CustomException(ErroEnums.APIERRO.getCode(), new Object[]{apiParam} , ErroEnums.APIERRO.getInfo()+r.getMsg());
    }

    public Map<String,Object> executeKdApi(String apiCode,Object param){
        ApiParam apiParam = new ApiParam();
        apiParam.setApiCode(apiCode);
        apiParam.setParam(param);
        R<Map<String,Object>> r = DispatchServiceHelper.invokeBizService("tcoiss", ServiceNameConstants.WEBAPI_SERVICE,"executeKdApi",apiParam);
        if(r.getCode()==200){
            return r.getData();
        }
        throw new CustomException(ErroEnums.APIERRO.getCode(), new Object[]{apiParam} , ErroEnums.APIERRO.getInfo()+r.getMsg());
    }

}
